package syntax;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;

public class TermArgument implements List<Term>, Comparable, Cloneable {
	private List<Term> args;
	private int hash = 0;

	public TermArgument() {
		this.args = new ArrayList<Term>();
		this.recomputeHash();
	}
	
	public TermArgument(int size) {
		this.args = new ArrayList<Term>(size);
		this.recomputeHash();
	}
	
	public TermArgument(Collection<Term> coll) {
		this.args = new ArrayList<Term>(coll);
		this.recomputeHash();
	}

	/**
	 * @param index
	 * @param element
	 * @see java.util.ArrayList#add(int, java.lang.Object)
	 */
	public void add(int index, Term element) {
		args.add(index, element);
	}

	/**
	 * @param o
	 * @return
	 * @see java.util.ArrayList#add(java.lang.Object)
	 */
	public boolean add(Term o) { // do poprawki Mateusz
		boolean flag = args.add(o);
		this.recomputeHash();
		return flag;
	}

	/**
	 * @param c
	 * @return
	 * @see java.util.ArrayList#addAll(java.util.Collection)
	 */
	public boolean addAll(Collection<? extends Term> c) {
		return args.addAll(c);
	}

	/**
	 * @param index
	 * @param c
	 * @return
	 * @see java.util.ArrayList#addAll(int, java.util.Collection)
	 */
	public boolean addAll(int index, Collection<? extends Term> c) {
		return args.addAll(index, c);
	}



	/**
	 * @param elem
	 * @return
	 * @see java.util.ArrayList#contains(java.lang.Object)
	 */
	public boolean contains(Object elem) {
		return args.contains(elem);
	}



	/**
	 * @param index
	 * @return
	 * @see java.util.ArrayList#get(int)
	 */
	public Term get(int index) {
		return args.get(index);
	}

	/**
	 * @return
	 * @see java.util.AbstractList#hashCode()
	 */
	public int hashCode() {
		//int old_hash = hash;
		//this.recomputeHash();
		/*
		if(old_hash != hash)  {
			System.out.println("CHUJ!!!");
		}*/
		return hash;
	}

	/**
	 * @return
	 * @see java.util.ArrayList#isEmpty()
	 */
	public boolean isEmpty() {
		return args.isEmpty();
	}

	/**
	 * @return
	 * @see java.util.AbstractList#iterator()
	 */
	public Iterator<Term> iterator() {
		return args.iterator();
	}

	/**
	 * @return
	 * @see java.util.AbstractList#listIterator()
	 */
	public ListIterator<Term> listIterator() {
		return args.listIterator();
	}

	/**
	 * @param index
	 * @return
	 * @see java.util.AbstractList#listIterator(int)
	 */
	public ListIterator<Term> listIterator(int index) {
		return args.listIterator(index);
	}

	/**
	 * @param index
	 * @return
	 * @see java.util.ArrayList#remove(int)
	 */
	public Term remove(int index) {
		return args.remove(index);
	}

	/**
	 * @param o
	 * @return
	 * @see java.util.ArrayList#remove(java.lang.Object)
	 */
	public boolean remove(Object o) {
		return args.remove(o);
	}

	/**
	 * @param index
	 * @param element
	 * @return
	 * @see java.util.ArrayList#set(int, java.lang.Object)
	 */
	public Term set(int index, Term element) {
		return args.set(index, element);
	}

	/**
	 * @return
	 * @see java.util.ArrayList#size()
	 */
	public int size() {
		return args.size();
	}

	/**
	 * 
	 * @see java.util.ArrayList#clear()
	 */
	public void clear() {
		args.clear();
	}

	/**
	 * @param c
	 * @return
	 * @see java.util.AbstractCollection#containsAll(java.util.Collection)
	 */
	public boolean containsAll(Collection<?> c) {
		return args.containsAll(c);
	}



	/**
	 * @param elem
	 * @return
	 * @see java.util.ArrayList#indexOf(java.lang.Object)
	 */
	public int indexOf(Object elem) {
		return args.indexOf(elem);
	}

	/**
	 * @param elem
	 * @return
	 * @see java.util.ArrayList#lastIndexOf(java.lang.Object)
	 */
	public int lastIndexOf(Object elem) {
		return args.lastIndexOf(elem);
	}

	/**
	 * @param c
	 * @return
	 * @see java.util.AbstractCollection#removeAll(java.util.Collection)
	 */
	public boolean removeAll(Collection<?> c) {
		return args.removeAll(c);
	}

	/**
	 * @param c
	 * @return
	 * @see java.util.AbstractCollection#retainAll(java.util.Collection)
	 */
	public boolean retainAll(Collection<?> c) {
		return args.retainAll(c);
	}

	/**
	 * @param fromIndex
	 * @param toIndex
	 * @return
	 * @see java.util.AbstractList#subList(int, int)
	 */
	public List<Term> subList(int fromIndex, int toIndex) {
		return args.subList(fromIndex, toIndex);
	}

	/**
	 * @return
	 * @see java.util.ArrayList#toArray()
	 */
	public Object[] toArray() {
		return args.toArray();
	}

	/**
	 * @param <T>
	 * @param a
	 * @return
	 * @see java.util.ArrayList#toArray(T[])
	 */
	public <T> T[] toArray(T[] a) {
		return args.toArray(a);
	}

	public int compareTo(Object that) {
		return this.compareTo((TermArgument) that);
	}
	
	public int compareTo(TermArgument that) {
		return TermMethods.compareArgsTo(this, that);
		/*
		int res = this.size() - that.size();
		
		if(res != 0)  {
			return res;
		}
		
		ListIterator<Term> iter1 = this.listIterator();
		ListIterator<Term> iter2 = that.listIterator();
		
		try  {
			while(true)  {
				res = iter1.next().compareTo(iter2.next());
				if (res != 0)  {
					return res;
				}
			}
		}
		catch(NoSuchElementException exception)  {
			return 0;
		}*/
	}
	
	public boolean equals(Object o) {
		return equals((TermArgument) o);
	}
	public boolean equals(TermArgument that) {
		if(this.hash != that.hash) return false;
		if(this == that) return true;
		return this.compareTo(that) == 0;
	}

	public String toString() {
		String res = "";
		for(Term term : args)  {
			res += term.toString() + "@@@@";
		}
		return res;
	}
	
	public void recomputeHash() {
		final int PRIME = 31;
		int result = 1;
		for(Term term : args)  {
			result = PRIME * result + term.hashCode();
		}
		
		hash = result;
	}
	
}
